package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ForgetPassword extends Activity implements OnClickListener {
	
	TextView username;
	TextView IDName;
	Button submit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forget_password);
		
		username = (TextView) findViewById(R.id.editText1);
		IDName = (TextView) findViewById(R.id.editText2);
		submit = (Button) findViewById(R.id.btnSubmit);
		submit.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.forget_password, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		BPEL_Process bpelProcess = new BPEL_Process();
		BPEL_Request request = new BPEL_Request();
		request.setUrl("http://webspace.apiit.lk/user/recover_password.jsp");

		Map<String, String> xpathMap = new HashMap<String, String>();
		xpathMap.put("value","/html[1]/body[1]/table[1]/tr[1]/td[1]/p[1]/font[1]---");
		request.setXpathMap(xpathMap);
		Map<String, String> parametersMap = new HashMap<String, String>();
		request.setParametersMap(parametersMap);
		request.setType("GET");
		BPEL_Response respones = null;
		try {
			respones = bpelProcess.doRequest(request);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		BPEL_Process bpelProcess2 = new BPEL_Process();
		BPEL_Request request2 = new BPEL_Request();
		request2.setUrl("http://webspace.apiit.lk/user/recover_password.jsp");

		Map<String, String> xpathMap2 = new HashMap<String, String>();
		xpathMap2.put("value","/html[1]/body[1]/table[1]/tr[1]/td[1]/font[1]/font[1]---");
		request2.setXpathMap(xpathMap2);
		Map<String, String> parametersMap2 = new HashMap<String, String>();
		parametersMap2.put("UserID", username.getText().toString());
		parametersMap2.put("ICPassport", IDName.getText().toString());
		parametersMap2.put("Submit", "Submit");
		request2.setParametersMap(parametersMap2);
		request2.setType("POST");
		BPEL_Response respones2 = null;
		try {
			respones2 = bpelProcess2.doRequest(request2);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String testResult = respones2.getResultsMap().get("value");
		Toast.makeText(getApplicationContext(), testResult,
				Toast.LENGTH_LONG).show();
		Intent i = new Intent(this, ForgetPasswordResult.class);
		i.putExtra("value", testResult);
		startActivity(i);
	}

}
